<template>
  <div>
    <a-card :bordered="false" class="card" title="基本信息">
      <span slot="extra">
        <a-button @click="$router.push({name: 'supplierInfo'})">返回</a-button>
      </span>
      <edit-form @notSubmit="handleNotSubmit" @sendData="handleSendData" ref="editForm" />
    </a-card>
    <a-card class="card" :bordered="false" title="营业执照信息">
      <edit-license @notSubmit="handleNotSubmit" @sendData="handleSendData" ref="editLicense" />
    </a-card>
    <a-card v-if="type === 'edit' || type === 'add'  " class="mb20" :bordered="false" title="业务员信息">
      <edit-people @notSubmit="handleNotSubmit" @sendData="handleSendData" ref="editPeople" />
    </a-card>
    <a-card
      v-if="type === 'add' || type === 'edit' || type === 'addType' "
      title="资质附件"
      :bordered="false"
    >
      <edit-file
        @notSubmit="handleNotSubmit"
        @addFile="handleAddFile"
        ref="editFile"
        @sendData="handleSendData"
      />
    </a-card>
    <file-modal @refresh="handleRefresh" ref="fileModal" />
    <div class="mt20 tl-center mb20">
      <a-button size="large" class="mr20 w100" @click="$router.push({name: 'supplierInfo'})">取消</a-button>
      <a-button size="large" @click="handleReset" class="mr20 w100">重置</a-button>
      <a-button
        size="large"
        type="primary"
        class="w100 mr20"
        @click="handleSubmit(false)"
      >{{ this.type === 'add' || this.type === 'addType' ? '新增' : '修改' }}</a-button>
      <a-tooltip placement="topLeft">
        <template slot="title">
          <span>点击新增供应商并直接进入准入申请</span>
        </template>
        <a-button class="w100" size="large" v-if="type === 'add'" @click="handleSubmit(true)">准入</a-button>
      </a-tooltip>
    </div>
  </div>
</template>

<script>
import EditForm from './EditForm'
import EditLicense from './License'
import EditPeople from './EditPeople'
// import EditComp from '@/components/EditPeople'
import EditFile from './EditFile'
import FileModal from './FileModal'
import { setSession, getSession, formatTime, removeSession } from '@/utils/util'
import { getSupplierInfoByCode, updateSupplierInfoAll, insertSupplierInfo, updateSupplierInfo, insertSupplierInfoType } from '@/api/supplier'
import { openNotification } from '@/utils/util'

export default {
  components: {
    EditForm,
    EditLicense,
    EditPeople,
    EditFile,
    // EditComp,
    FileModal
  },
  data() {
    return {
      editFormData: {},
      editLicenseData: {},
      editPeopleData: {},
      editFileData: [],
      fileData: [],
      isEdit: false,
      type: '',
      mdl: {},
      firstTrue: true,
      secondTrue: true,
      thirdTrue: true,
      fourthTrue: true
    }
  },
  mounted() {
    console.log(this.$route)
    if (JSON.stringify(this.$route.params) !== '{}') {
      this.mdl = this.$route.params.obj
      this.type = this.$route.params.type
      setSession('record', this.mdl)
      setSession('type', this.$route.params.type)
    } else {
      this.mdl = getSession('record')
      this.type = getSession('type')
    }
    console.log('type', this.type)
    // 编辑获取资质附件和业务员数据
    this.getInitialData()
    this.$refs.editForm.edit(this.type, this.mdl)
  },
  methods: {
    handleRefresh(arr) {
      console.log('index 接收 arr', arr)
      this.$refs.editFile.saveData(arr)
      if (this.type === 'edit' || this.type === 'addType') {
        // this.getInitialData()
      }
    },
    getInitialData() {
      if (this.type !== 'add') {
        getSupplierInfoByCode(this.mdl.supplierCode).then(res => {
          console.log('code info res', res, res.data.supplierAccessory)
          this.fileData = res.data.supplierAccessory
          const { supplierLinkman } = res.data
          if (this.type === 'edit' || this.type === 'addType') {
            this.$refs.editFile.edit(this.fileData)
          }
          this.$refs.editLicense.edit(this.type, { ...this.mdl, savePath: res.data.supplierInfo.savePath })
          if (this.type === 'edit') {
            this.$refs.editPeople.edit(this.type, supplierLinkman)
          }
        })
      }
    },
    handleAddFile() {
      this.$refs.fileModal.edit(false, {})
    },
    // 子组件调用父组件这个方法，把form的值全部放到这个文件里面来
    handleSendData(value, num) {
      switch (num) {
        case 1:
          this.editFormData = Object.assign({}, value)
          this.firstTrue = true
          break
        case 2:
          this.editLicenseData = Object.assign({}, value)
          this.secondTrue = true
          break
        case 3:
          this.editPeopleData = Object.assign({}, value)
          this.thirdTrue = true
          break
        case 4:
          this.editFileData = [...value]
          this.fourthTrue = true
          break
        default:
          break
      }
    },
    // 页面上有信息未填写完整
    handleNotSubmit(bool, num) {
      if (num === 1) {
        this.firstTrue = bool
      } else if (num === 2) {
        this.secondTrue = bool
      } else if (num === 3) {
        this.thirdTrue = bool
      } else {
        this.fourthTrue = bool
      }
    },
    async handleSubmit(access = false) {
      this.$refs.editForm.getFormData()
      this.$refs.editLicense.getFormData()
      if (this.type === 'edit' || this.type === 'add') {
        this.$refs.editPeople.getFormData()
      }
      if (this.type === 'add' || this.type === 'edit' || this.type === 'addType') {
        this.$refs.editFile.getFormData()
      }
      if (this.firstTrue && this.secondTrue && this.thirdTrue && this.fourthTrue) {
        let param = {}
        param.supplierAccessory = [...this.editFileData]
        param.supplierInfo = { ...this.editFormData, ...this.editLicenseData }
        param.supplierLinkman = { ...this.editPeopleData }
        console.log('param', param)
        let mes = ''
        let res = ''
        if (this.type === 'edit') {
          // 编辑
          res = await updateSupplierInfoAll(param)
          mes = '修改操作成功'
        } else if (this.type === 'add') {
          console.log('新增 type', this.type, access)

          // 新增
          if (access) {
            res = await insertSupplierInfo({ ...param, entryState: 0 })
            mes = '新增供应商并准入成功'
          } else {
            res = await insertSupplierInfo({ ...param, entryState: 1 })
            mes = '新增供应商成功'
          }
        } else if (this.type === 'change') {
          // 供应商变更
          res = await updateSupplierInfo(param.supplierInfo)
          mes = '供应商变更成功'
        } else if (this.type === 'addType') {
          console.log('addType')
          // 新增供应商类型
          res = await insertSupplierInfoType({ supplierAccessory: param.supplierAccessory, supplierInfo: param.supplierInfo })
          mes = '新增供应商类型成功'
        }

        if (res.state === '000000') {
          openNotification('success', mes)
          this.$router.push({
            name: 'supplierInfo'
          })
        } else {
          openNotification('error', '操作失败')
        }
      } else {
        this.$message.error('请注意填写页面必填项')
      }
    },
    handleReset() {
      const _this = this
      this.$confirm({
        title: '重置操作',
        content: `该操作将清空所有表单并删除已经上传的图片，您确定要重置所有记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        onOk() {
          console.log('111')
          // 调用删除接口，刷新表格数据
          _this.$refs.editForm.reset()
          _this.$refs.editLicense.reset()
          _this.$refs.editPeople.reset()
        }
      })
    }
  },
  destroyed() {
    removeSession('record')
    removeSession('type')
  }
}
</script>

<style scoped>
.card {
  margin-bottom: 20px;
}
.w100 {
  width: 100px;
}
</style>